﻿using System;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
using System.Collections.Generic;
namespace CSharp5
{
    public  class OldCode
    {
        /// <summary>
        /// avoid magic string, and keep string updated when paramter name change
        /// </summary>
        /// <param name="para1"></param>
        /// <param name="para2"></param>
        public static void DoNameOf(string para1, string para2 )
        {
            if (string.IsNullOrEmpty(para1) || string.IsNullOrEmpty(para2))
                throw new ArgumentNullException(string.Join(",","para1","para2"), "Method Parameters can not be null"); //
        }

        public static void DoExceptionFilter() {

            try
            {
                throw new Exception("My Exception");
            }
            catch (Exception ex)
            {
                if (ex.Message.Equals("My Exception", StringComparison.InvariantCultureIgnoreCase))
                {
                   Console.WriteLine("Log my exception"); //
                }

                throw;
            }
        }



        #region USE await in catch and finally block
        private static async Task LongTimeWorkMayThrowException()
        {
            Thread.Sleep(10000);
            throw new TimeoutException("Sorry, opration time out");
        }
        private static async Task LogErrorAsync(Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        private static async Task<bool> CleanupResource()
        {
            //do sth to clean up resource
            return true;
        }
        public static async void DoTryCatchFinallyForAwaitAsync()
        {
            TimeoutException exp=null;
            try
            {
                await LongTimeWorkMayThrowException();
            }
            catch (TimeoutException ex)
            {
                exp = ex;
            }
            if(exp!=null)
                await LogErrorAsync(exp);
        }
        #endregion


        public void GenerateTeam()
        {
            Person Brian = new Person("Brian", "O'Rouke", "Mgr"); 
            Team tablet = new Team("Tablet",Brian);
            
         
        }

    }

   

    public class Team
    {
        public string Name { get; set; }
        public Person Lead { get; private set; }
        public IEnumerable<Person> TeamMembers { get; set; }
        public Team(string name, Person lead) {
            Name = name;
            Lead = lead;
        }
        public Team(string name, Person lead, IEnumerable<Person> team) :this(name,lead)
        {
            TeamMembers = team;
        }
    }

    public struct Person
    {
        public string FirstName { get;}
        public string LastName { get;}

        public string Postion { get; set; }

        public Person(string fname, string lname, string pos) {
            FirstName = fname;
            LastName = lname;
            Postion = pos;
        }
    }
}
